package dao

import (
	"album/model"
	"context"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
)

type BaseCategory2Dao struct {
	conn *gorms.GormConn
}

func (u *BaseCategory2Dao) FindCategory2ById(ctx context.Context, id int64) (category2 *model.BaseCategory2, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory2{}).Where("id = ?", id).First(&category2).Error
	if err != nil {
		return nil, err
	}
	return category2, nil

}
func (u *BaseCategory2Dao) FindByCategory2ByOrder(ctx context.Context, category1Id int64) (category2s []*model.BaseCategory2, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory2{}).Where("category1_id = ?", category1Id).Order("order_num desc").Find(&category2s).Error
	if err != nil {
		return nil, err
	}
	return category2s, nil
}

func NewBaseCategory2Dao(db *db.ListenDB) *BaseCategory2Dao {
	return &BaseCategory2Dao{
		conn: gorms.New(db.Conn),
	}
}
func (u *BaseCategory2Dao) FindByCategory2(ctx context.Context, category1Id int64) (category2s []*model.BaseCategory2, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseCategory2{}).Where("category1_id = ?", category1Id).Find(&category2s).Error
	if err != nil {
		return nil, err
	}
	return category2s, nil
}

//func (u *BaseCategory2Dao) FindByCategory1ToC2Id(ctx context.Context, c1Id int64) (category2s *model.BaseCategory2, err error) {
//
//}
